package org.surthi.app.bank.service;

import java.util.HashSet;
import java.util.Set;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.surthi.app.bank.db.dao.AccountDao;
import org.surthi.app.bank.db.dao.AccountHolderDao;
import org.surthi.app.bank.db.dao.BankDao;
import org.surthi.app.bank.db.dao.TransactionCallbackWithResult;
import org.surthi.app.bank.db.dvo.Account;
import org.surthi.app.bank.db.dvo.FixedDeposit;
import org.surthi.app.bank.web.bean.FixDepositView;

public class AccountServiceImpl implements AccountService{

	private TransactionTemplate transactionTemplate;
	
	private AccountDao accountDao;
	private BankDao bankDao;
	private AccountHolderDao accountHolderDao;

	public FixDepositView displayAccountHolderInformation(final String accountId) {
		FixDepositView fixDepositView = null;
		
		/*fixDepositView = (FixDepositView) transactionTemplate.execute(new TransactionCallbackWithResult() {
			
			@Override
			protected Object doInTransactionWithResult(
					TransactionStatus transactionStatus) {*/
				Account account = accountDao.findAccount(accountId);
				FixDepositView innerFixDepositView = new FixDepositView();
				innerFixDepositView.setAccountId(account.getAccountId());
				innerFixDepositView.setAccountHolderName(account.getAccountHolder().getAccountHolderName());
				innerFixDepositView.setBankName(account.getBank().getBankName());
				innerFixDepositView.setAccountIds(accountDao.getAllAccountNumbers());
				/*return innerFixDepositView;
			}
		});*/
				fixDepositView = innerFixDepositView;
		return fixDepositView;
	}
	
	public void saveAccountHolderInformation(final FixDepositView fixDepositView) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
				// TODO Auto-generated method stub
				FixedDeposit fixedDeposit = new FixedDeposit();
				fixedDeposit.setFixedDepositNumber(fixDepositView.getFixDepositId());
				fixedDeposit.setAmount(fixDepositView.getAmount());
				fixedDeposit.setFixedDepositStatus(fixDepositView.getStatus());				
				Account account = accountDao.findAccount(fixDepositView.getAccountId().trim());
				if (account != null ){
					System.out.println("Found account with out number : "+ account.getAccountId());
				}
				Set fixDeposits = account.getFixedDeposits();
				if (fixDeposits == null){
					fixDeposits = new HashSet();
				}
				
				fixedDeposit.setAccount(account);
				fixDeposits.add(fixedDeposit);				
				account.setFixedDeposits(fixDeposits);
				
				accountDao.updateAccount(account);
				System.out.println("Fixed Deposit Updated");
			}
		});
	}
	
	public void setTransactionManager(PlatformTransactionManager transactionManager){
		this.transactionTemplate = new TransactionTemplate(transactionManager);
	}
	public AccountDao getAccountDao() {
		return accountDao;
	}
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	public BankDao getBankDao() {
		return bankDao;
	}
	public void setBankDao(BankDao bankDao) {
		this.bankDao = bankDao;
	}
	public AccountHolderDao getAccountHolderDao() {
		return accountHolderDao;
	}
	public void setAccountHolderDao(AccountHolderDao accountHolderDao) {
		this.accountHolderDao = accountHolderDao;
	}
}
